package com.dgut.legalservice.dao;

import com.dgut.legalservice.domain.OrderVo;
import com.dgut.legalservice.domain.Product;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Mapper
public interface IProduct extends tk.mybatis.mapper.common.Mapper<Product> {

    /**
     * 添加产品
     * @param product
     * @return
     */
    @Insert("insert into t_product(productName,productType,price,status,createTime,updateTime,createBy,updateBy,description) values (#{productName},#{productType},#{price},#{status},#{createTime}," +
            "#{updateTime},#{createBy},#{updateBy},#{description})")
    @SelectKey(keyColumn="id",keyProperty="id",resultType=Integer.class,before =
            false, statement = { "select last_insert_id()" })
    public int addProduct(Product product);

    /**
     * 删除产品
     * @param id
     * @return
     */
    @Delete("delete from t_product where id = #{id}")
    public int deleteProduct(Integer id);


    /**
     * 更新产品
     * @param product
     * @return
     */
    @Update("update t_product set productName = #{productName},productType = #{productType},price = #{price}," +
            "status = #{status},createTime = #{createTime},updateTime = #{updateTime},createBy = #{createBy},updateBy = #{updateBy},description = #{description}" +
            " where id = #{id}")
    public int updateProduct(Product product);

    /**
     * 查询所有产品
     * @param
     * @return
     */
    @Select("select * from t_product")
    public List<Product> findProduct();

    /**
     * 关联查询 多对多
     * @param map
     * @return
     */
//    @Select("select * from t_product where id in(select productId from t_product_real where orderId = #{id}) and productName = #{productName})")
    @SelectProvider(type = OrderPro.class,method = "selectOrder")
    public Product findProductById(Map<String,Object> map);

    class OrderPro{
        public String selectOrder(Map<String,Object> map){
            return new SQL(){{
                SELECT("*");
                FROM("t_product");
                if(map.get("id")!=null){
                    WHERE("id in(select productId from t_product_real where orderId = #{id})");
                }
                if (map.get("productName")!=null){
                    WHERE("productName = #{productName}");
                }
            }}.toString();
        }
    }
    /**
     * 通过id查询产品
     * @param id
     * @return
     */
    @Select("select * from t_product where id = #{id}")
    public Product findId(Integer id);

    /**
     * 查找合同服务
     * @return
     */
    @Select("select * from t_product where productType = '合同服务'")
    public List<Product> findContract();
}
